export const HOOKS = [
  "beforeCreate",
  "created",
  "beforeMount",
  "mounted",
  "beforeUpdate",
  "updated",
  "beforeDestory",
  "destoryed",
];
//策略模式
let starts = {};
starts.data = function (parentVal, childVal) {
  return childVal;
};
// starts.data = function () {}; //合并data
// starts.computed = function () {}; //合并computed
// starts.watch = function () {}; //合并watch
// starts.methods = function () {};

function mergeHook(parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal); //第二次 [a(){}，b(){}]
    } else {
      return [childVal];
    }
  } else {
    return parentVal;
  }
}
HOOKS.forEach((hooks) => {
  starts[hooks] = mergeHook;
});

export function mergeOptions(parent, child) {
  // Vue.option = {created:[a,b,c],watch:[a,b]}

  const options = {};
  for (let key in parent) {
    mergField(key);
  }
  //儿子有 父亲没有
  for (let key in child) {
    mergField(key);
  }
  function mergField(key) {
    //根据key  策略模式

    if (starts[key]) {
      options[key] = starts[key](parent[key], child[key]); //undefined  created()
    } else {
      options[key] = child[key];
    }
  }

  return options;
}
